var refurbishQr = null;
$(function() {
//	$.get(contextPath + "/account/employeeAdminJsonQr", function(result) {
//		if (result.success) {
//			if (result.picUrl != null) {
//				var pic = "<img src=\"" + result.picUrl + "\" width='200px;'>";
//				$("#employeeQrCode").html(pic + "</br>扫码关联微信帐号");
//			}
//		} else {
//			$.messager.alert('提示', result.msg, 'warning');
//		}
//	});
	canBinding();
	
	
	$("#employeeQrDialog").dialog({
		onClose: function () {
			if (refurbishQr) {
				clearInterval(refurbishQr);
			}
		}
	});

});

function canBinding(){
	$('#employeeQrCode').html("");
	if (refurbishQr) {
		clearInterval(refurbishQr);
	}
	
	$.ajax({
		type : "get",
		url : contextPath + "/account/employeeAdminCanBinding",
		async : false,
		success : function(result) {
			if (result.success) {
				$("#canBinding").show();
			}
			
			if(result.loginMode == "2" || result.loginMode == "3") {
				if(result.binding == false){
					$("#employeeQrDialog").dialog({
						modal: true
					});
					setTimeout("openBindingDialog()",1000);
					//openBindingDialog();
				}
			}
		},
		error : function() {
		}
	});
	
	
}

function reloadBindingDialog(){
	$("#employeeQrDialog").dialog("close");
	openBindingDialog();
}
function openBindingDialog(){
	var qrPic = "";
	$.ajax({
		type : "get",
		url : contextPath + "/account/employeeAdminViewBindingQr",
		async : false,
		success : function(rs) {
			
			if (rs.success) {
				var qrVo = rs.qrVo;
				var url = contextPath + "/file/viewQrImg?pic=" + qrVo.imgPath + "&code=" + qrVo.code + "&name=" + qrVo.name;
				qrPic += "<img src='" + url + "' width='350px'/><br/>" +qrVo.name + "<br/>";
				
				refurbishQr = setInterval(function() {
					$.ajax({
						type : "get",
						url: contextPath + "/account/employeeAdminBindingQrCheck",
						async : false,
						success : function(rss) {
							if(rss.success == true) {
								$("#employeeQrCode").html("<a href='#' onclick='reloadBindingDialog();'>用户已扫码，点击重新加载二维码</a>");
								if (refurbishQr) {
									clearInterval(refurbishQr);
								}
							}
						}
					});
				}, 1000);
				
			} else {
				qrPic = "<a href='#' onclick='reloadBindingDialog();'>" + rs.msg + "</a>";
			}
		},
		error : function() {
			// 请求出错处理
			qrPic = "提示', '出现异常，请稍后再试";
		}
	});
	$('#employeeQrCode').html(qrPic);
	$("#employeeQrDialog").dialog("open").dialog("setTitle", "扫码绑定用户微信");
	
}